package com.guanganren.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.guanganren.db.entity.User;
import com.guanganren.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{

	private UserService userService;
	private String username;
	private String password;
	private String querenpassword;
	private String shouji;
	private String email;
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getQuerenpassword() {
		return querenpassword;
	}

	public void setQuerenpassword(String querenpassword) {
		this.querenpassword = querenpassword;
	}

	public String getShouji() {
		return shouji;
	}

	public void setShouji(String shouji) {
		this.shouji = shouji;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String doIt(){
		 System.out.println("doIt---------------start-----");
		return SUCCESS;
	}
	public String execute() {
		System.out.println("-------TestAction--------START--");
		
		//ServletActionContext.getRequest().setAttribute("ui", bbsService.getUser("asdf"));
		User user = userService.getUser("");

		System.out.println("-------TestAction--------END--");
		return SUCCESS;
	}
	public String initReg(){
		return "initregist";
	}
	public String initLog(){
		return "initlogin";
	}
	public String check(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		String criteraType = request.getParameter("type");
		
		try {
			String critera = java.net.URLDecoder.decode(request.getParameter(criteraType),"UTF-8");
			List<User> list = userService.getExistUser(criteraType, critera);
			if(list.size()==0){
				response.getWriter().write("true");
			}else{
				response.getWriter().write("false");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public String regist(){
		//------------------验证-------------------------------------
		
		
		
		
		
		//------------------验证-------------------------------------
		User user = new User(username,password,shouji,email,"http://localhost:8080/img/img01.jpg");
		try{
			userService.addUser(user);
		}catch(Exception ex){
			ex.printStackTrace();
			return "initregist";
		}
		return "regist";
	}
	public String login(){
		try{
			User user = userService.getUser(username);
			if(password.equals(user.getUserPwd())){
				HttpSession session = ServletActionContext.getRequest().getSession();
				session.setAttribute("user",user);
			}
		}catch(Exception ex){
			ex.printStackTrace();
			return "initlogin";
		}
		return "login";
	}
}
